package com.myBlog.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.myBlog.pojo.Article;
import com.myBlog.repository.ArticleMapper;
import com.myBlog.repository.UserMapper;
import com.myBlog.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/test")
public class ControllerTest {

    @Autowired
    StringRedisTemplate strRedis;


    @Autowired
    private UserMapper userMapper;


    @GetMapping("/test")
    public List test(@RequestParam(value = "name", defaultValue = "World") String name) {
        log.info("----- selectAll method test ------");
        strRedis.opsForValue().set("name","jinxiaozhi");
        strRedis.opsForValue().set("age","25");
        return null;
    }

    @RequestMapping("/redis")
    public String redis(@RequestParam(value = "key",defaultValue = "name")String name) throws Exception {
        log.debug("get redis");
        return  strRedis.opsForValue().get(name);
    }

    @Autowired
    private ArticleMapper articleMapper;

    @RequestMapping("/articleAll")
    public List articleAll(){
       return articleMapper.selectList(null);
    }


    // Map接受json请求参数，json注意加@RequestBody，其他请求不需要，可以直接使用Map(formData请求里 拿不到文件)
    @PostMapping("/test-map")
    public JSONObject testMap(@RequestBody Map map){
        System.out.println(map);
        return JsonUtil.ok_info("操作正常");
    }

    // 也可以直接将请求体转为 JSONObject
    @PostMapping("/test-json")
    public JSONObject testJSON(@RequestBody JSONObject req){
        System.out.println(req);
        return JsonUtil.ok_with_Data(req);
    }
}
            